package com.zrrd.yunchmall.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.user.entity.Resource;
import com.zrrd.yunchmall.user.entity.ResourceCategory;
import com.zrrd.yunchmall.user.service.IResourceService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台资源表 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private IResourceService service;

    @GetMapping("/list")
    @ApiOperation("查询资源列表")
    public ResponseResult list(String urlKeyword, String nameKeyword, Integer categoryId
            , @RequestParam int pageSize, @RequestParam int pageNum) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(urlKeyword)) {
            queryWrapper.like("url", urlKeyword);
        }
        if (!StringUtils.isEmpty(nameKeyword)) {
            queryWrapper.like("name", nameKeyword);
        }
        if (categoryId != null) {
            queryWrapper.eq("category_id", categoryId);
        }
        return new ResponseResult<>(200, "查询成功!", service.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
    @PostMapping("/update/{id}")
    @ApiOperation("编辑资源")
    public ResponseResult update(@PathVariable("id") Integer id,@RequestBody Resource resource){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        return new ResponseResult<>(200,"查询成功!",service.update(resource,updateWrapper));
    }
    @PostMapping("/delete/{id}")
    @ApiOperation("删除资源")
    public ResponseResult delete(@PathVariable("id") Integer id){
        return new ResponseResult<>(200,"删除成功!",service.removeById(id));
    }
    @PostMapping("/create")
    @ApiOperation("添加资源")
    public ResponseResult create(@RequestBody Resource resource){
        return new ResponseResult<>(200,"查询成功!",service.save(resource));
    }
}
